<?php
/**
 * Created by PhpStorm.
 * User: Happy
 * Date: 2016/1/11 0011
 * Time: 21:09
 */
//输出信息 lcc  2016-3-22
function println($msg=''){
   // echo chr('10').$msg;
}
function write_file($data, $file_path = 'a.php')
{

    if (!is_array($data) && !is_scalar($data)) {
        return false;
    }
    $data = var_export($data, true);
    $data = "<?php return " . $data . ";";
    //写独占锁
    $fp = fopen($file_path , 'w');
    if(flock($fp , LOCK_EX)){
        fwrite($fp ,$data);
        flock($fp , LOCK_UN); //写完解锁
    }
    fclose($fp);
   // return file_put_contents($file_path, $data);
}



function  write($val, $file = 'a.html')
{
    $fh = fopen($file, "a+");

    if (is_array($val)) {
        $val = var_export($val, true);//如果是数组
    }
    fwrite($fh, chr(10) . $val . '<br/>' . chr(10));    //
    fclose($fh);
}
//打印堆栈信息
function print_stack(){
header("Content-type: text/html; charset=utf-8");
$array =debug_backtrace();
//print_r($array);//信息很齐全
unset($array[0]);
$html ='';
foreach($array as $row)
{
    $html .=$row['file'].':'.$row['line'].'行,调用方法:'.$row['function']."<p>";
}
echo $html;
}

/**
 * 结束测试，并把结果写入文件中
 * 计算起始时间差
 **/
function  time_end($start_time = 0, $info = "")
{
    //  $start_time= microtime(true);//时间戳加微妙  这句话加在开始

    //echo (microtime(true)-$start_time)*1000,'ms';
    $diff = round((microtime(true) - $start_time) * 1000, 4) . 'ms';
    write($info . ' :' . $diff);//写入文件
    return $diff;
}

/**
 * 循环创建目录
 *
 * @param string $dir 待创建的目录
 * @param  $mode 权限
 * @return boolean
 */
function mk_dir($dir, $mode = '0777')
{
    if (is_dir($dir) || @mkdir($dir, $mode))
        return true;
    if (!mk_dir(dirname($dir), $mode))
        return false;
    return @mkdir($dir, $mode);
}


function mk_uploads_path($root='',$nameExt='png',&$filename){

    if(!$root){$root=BASE_PATH;}
    //生成文件夹名和路径
    $v_path = date('Y'); //年
    if(!is_dir($root.'/'.$v_path)) {
        mkdir($root.'/'.$v_path, 0777);
        chmod($root.'/'.$v_path, 0777);
    }
    $v_path .= '/'.date('m'); //月
    if(!is_dir($root.'/'.$v_path)) {
        mkdir($root.'/'.$v_path, 0777);
        chmod($root.'/'.$v_path, 0777);
    }
    $v_path .= '/'.date('d');//日
    if(!is_dir($root.'/'.$v_path)) {
        mkdir($root.'/'.$v_path, 0777);
        chmod($root.'/'.$v_path, 0777);
    }
    $path=$root.'/'.$v_path;
    file_put_contents($path.'/index.php',''); //放置index文件放置文件以列表展现
    $v_filename =randomString(16, 'abcdefghijklmnopqrstuvwxyz0123456789').'.'.$nameExt;
    while(is_file($path.'/'.$v_filename)) {//如果有重名文件则重新生成随机串值
        $v_filename = randomString(16, 'abcdefghijklmnopqrstuvwxyz0123456789').'.'.$nameExt;
    }
    $filename=$v_path.'/'.$v_filename;
    return $path.'/'.$v_filename;
}

function randomString($length, $source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
    $sourceLength = strlen($source);
    $temp = '';
    for ($i = $length; $i; $i--)
        $temp .= $source[rand(0, $sourceLength - 1)];
    return $temp;
}
//获取数据
function I_2($key){
  if(isset($_GET[$key])){
      return  $_GET[$key];
  }
   else if(isset($_POST[$key])){
       return  $_POST[$key];
   }
    else {
        return '';//没有则返回空
    }
}

//获取一个control
function Control($control){
   include_once(BASE_PATH.'/control/control.php');

    static $_cache = array();
    if (isset($_cache[$control])) {
        return $_cache[$control];
    }

    $file_name = BASE_PATH . '/control/' . $control . '.php';

    if (!$control) //如果为空或null直接返回model对象
    {
        return new Control();
    }
    $class_name = $control . 'Control';

    if (class_exists(@$class_name, false)) { //由于测试是indexControl已经加载过但不是这个方法加载过的，所以没在静态缓存中
        return $_cache[$control] = new $class_name();
    }
    include($file_name); //动态引入文件
    if (!class_exists($class_name)) {
        $error = 'Control Error:  Class ' . $class_name . ' is not exists!';
        throw new Exception($error);
    } else {
        return $_cache[$control] = new $class_name();
    }

}
//设置文件缓存的值  第三个参数为设置过期时间 单位为秒
function set_file_cache($key, $value, $expire = 0,$path='',$ext='.html')
{
    // xdebug_print_function_stack();
    if(!$path){
        $path= BASE_PATH.'/cache';
    }
    if (!is_dir($path)) {
        mkdir($path);
    }
    $cache_path = $path . '/' . $key . $ext;
    if (is_array($value)) {
        $value = serialize($value);
    }

    file_put_contents($cache_path, $value);

    //计算过期时间
    if ($expire != 0) {
        $time_path = $path . '/' . $key . '_time'.$ext;
        $expire_time = time() + $expire; //变成毫秒
        file_put_contents($time_path, $expire_time);// 放置过期时间
    }
    return true;
}

//获取文件缓存的值  获取文件，过期返回false
function  get_file_cache($key,$path='',$ext='.html')
{
    if(!$path){
        $path= BASE_PATH.'/cache';
    }
    $cache_path = $path . '/' . $key . $ext;
    $time_path = $path . '/' . $key . '_time'.$ext;

    if (file_exists($time_path)) {
        $time = file_get_contents($time_path);
        if ($time == 0 || $time > time()) { //如果永不过期或者还没有过期 读取文件
            if (file_exists($cache_path)) {
                $content = file_get_contents($cache_path);
                if (!$content || strlen($content) < 2) {
                    return $content;
                }
                if (substr($content, 0, 2) == 'a:') {//数组需要反序列化
                    return unserialize($content);
                } else {
                    return $content;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    else {
        if (file_exists($cache_path)) {
            $content = file_get_contents($cache_path);
            if (!$content || strlen($content) < 2) {
                return $content;
            }
            if (substr($content, 0, 2) == 'a:') {//数组需要反序列化
                return unserialize($content);
            } else {
                return $content;
            }
        } else {
            return false;
        }
    }
}

